<?php echo form_tag('mr/save', array(
  'id'        => 'sf_admin_edit_form',
  'name'      => 'sf_admin_edit_form',
  'multipart' => true,
)) ?>

<?php echo object_input_hidden_tag($mr, 'getId') ?>

<fieldset id="sf_fieldset_none" class="">

<div class="form-row">
  <?php echo label_for('mr[mrno]', __($labels['mr{mrno}']), 'class="required" ') ?>
  <div class="content<?php if ($sf_request->hasError('mr{mrno}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{mrno}')): ?>
    <?php echo form_error('mr{mrno}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>

  <!--Tien-->
  <?php $default = CommonValues::GenerateAutoNumber('MR', $mr->getMrno(), 'MR')   ?>
   
   <input name="mr_mrno_show" id="mr_mrno_show" value="<?php echo $default ?>" size="15" disabled="disabled" type="text">
   </div>
</div>

<div class="form-row">
  <?php echo label_for('mr[bom_id]', __($labels['mr{bom_id}']), 'class="required" ') ?>
  <div class="content<?php if ($sf_request->hasError('mr{bom_id}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{bom_id}')): ?>
    <?php echo form_error('mr{bom_id}', array('class' => 'form-error-msg')) ?>
  <?php endif;
  
  ?>

      <select name="mr[bom_id]" id="mr_bom_id" onchange="changeList()">
          <option selected ="selected"></option>
  <?php
  $c = new Criteria();
  $c->add(BOMPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
  $c->addDescendingOrderByColumn(BOMPeer::WONO);
  $bomlist = BOMPeer::doSelect($c);
  if ($bomlist && count($bomlist)){
      foreach($bomlist as $bom1){
          $bomid = $bom1->getId();
          $selected ="";          
          if ($mr->getBomId() && $bomid == $mr->getBomId())
              $selected ='selected="selected"';
          
          echo "<option value='$bomid' $selected>";
          echo $bom1->getWono();
          echo ":";
          echo $bom1->getProject();
          echo "</option>";
      }
  }
  ?>

</select>
    </div>
</div>

<div class="form-row">
  <?php echo label_for('mr[created_at]', __($labels['mr{created_at}']), '') ?>
  <div class="content<?php if ($sf_request->hasError('mr{created_at}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{created_at}')): ?>
    <?php echo form_error('mr{created_at}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>

  <?php $value = object_input_date_tag($mr, 'getCreatedAt', array (
  'rich' => true,
  'calendar_button_img' => '/sf/sf_admin/images/date.png',
  'control_name' => 'mr[created_at]',
)); echo $value ? $value : '&nbsp;' ?>
    </div>
</div>


<div class="form-row">
  <?php echo label_for('mr[reqarrdate]', __($labels['mr{reqarrdate}']), '') ?>
  <div class="content<?php if ($sf_request->hasError('mr{reqarrdate}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{reqarrdate}')): ?>
    <?php echo form_error('mr{reqarrdate}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>

  <?php $value = object_input_date_tag($mr, 'getReqarrdate', array (
  'rich' => true,
  'calendar_button_img' => '/sf/sf_admin/images/date.png',
  'control_name' => 'mr[reqarrdate]',
)); echo $value ? $value : '&nbsp;' ?>
    </div>
</div>

<div class="form-row">
  <?php echo label_for('mr[category]', __($labels['mr{category}']), '') ?>
  <div class="content<?php if ($sf_request->hasError('mr{category}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{category}')): ?>
    <?php echo form_error('mr{category}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>
     
    
    <input type="text" name ="mr_show" id ="mr_show" disabled="1"  value="Project">
  <input type="hidden" name="mr[category]" id="mr_category" value="1">
    </div>
</div>

<div class="form-row">
  <?php echo label_for('mr[status]', __($labels['mr{status}']), '') ?>
  <div class="content<?php if ($sf_request->hasError('mr{status}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{status}')): ?>
    <?php echo form_error('mr{status}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>
    <?php  $status = $mr->getStatus()?  $mr->getStatus(): 0?> 
  <input type="text" name ="mr_show" id ="mr_show" disabled="1"  value="<?php echo CommonValues::$gMRStatus['value'][$status]?>">
  <input type="hidden" name ="mr[status]" id ="mr_status" value="<?php echo $status?>">
    </div>
</div>

<div class="form-row">
  <?php echo label_for('mr[approvalstatus]', __($labels['mr{approvalstatus}']), '') ?>
  <div class="content<?php if ($sf_request->hasError('mr{approvalstatus}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{approvalstatus}')): ?>
    <?php echo form_error('mr{approvalstatus}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>

  <?php  $status = $mr->getApprovalstatus()?  $mr->getApprovalstatus(): 0?> 
  <input type="text" name ="mr_show" id ="mr_show" disabled="1"  value="<?php echo CommonValues::$gMRApprovalStatus['value'][$status]?>">
  <input type="hidden" name ="mr[approvalstatus]" id ="mr_approvalstatus" value="<?php echo $status?>">
    </div>
</div>

</fieldset>

<!--Tien code here
get information for MR-->
<?php
    if ($mr->getComponentgroupId()){
        $componentGroupId = $mr->getComponentgroupId();
        
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID , $componentGroupId);
        $c->addAscendingOrderByColumn(ComponentsPeer::COMPONENT_ID);
        
        $componentList = ComponentsPeer::doSelect($c);        
    }
?>


<?php 
echo link_to_remote('', array('update' => 'list_component', 
  'url' => 'component/listCompByBOMAjax',
  'loading' => visual_effect('appear', 'indicator'),
  'complete' => visual_effect('fade', 'indicator'). visual_effect('highlight', 'list_component'),
  'position' => 'bottom',
));
?>

<div id="sf_admin_bar">

<div class="sf_admin_filters">	  
    <div id="indicator" style="display: none">Loading...</div>    
</div>
</div>  	    

<div id ="list_component">
          <table id ='ListOfComponents' "cellspacing="0" class="sf_admin_list">
            <thead>
            <tr>
                  <th id="sf_admin_list_th_productcode" width="20%">Product Code
                          </th>
                  <th id="sf_admin_list_th_description" width="30%">Description
                          </th>
                  <th id="sf_admin_list_th_description" width="10%">Brand
                          </th>
                  <th id="sf_admin_list_th_description" width="10%">Unit
                          </th>
                  <th id="sf_admin_list_th_description" width="10%">Exp Dlv Date
                          </th>
                  <th id="sf_admin_list_th_description">Qty to request</th>
            </tr>
            </thead>
          
<!--show component here-->
<?php if (isset($mr->rowIds) && count($mr->rowIds)) {
    foreach ($mr->rowIds as $key=>$id) { 
        $component1= ComponentPeer::retrieveByPK($id);?>
        <tr class="sf_admin_row_<?php echo $key%2?>">
            <input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="<? echo $component1->getId()?>"/>
            <input type="hidden" name="expdates[]" id ="expdates" value ="<?echo $mr->expdates[$key]?>">
            <td><?echo $component1->getProductcode()?></td>
            <td><?echo $component1->getDescription()?></td>
            <td><?echo $component1->getBrand()?></td>
            <td><?echo $component1->getComponentunit()?></td>
            <td><?echo $mr->expdates[$key]?></td>
            <td><input type="text" name="qtys[]" id ="qtys" size="5" value ="<?echo $mr->qtys[$key]?>"></td>
        </tr>
<?} } elseif ( isset($componentGroupId) && $componentGroupId && isset($componentList) && $componentList) {
    foreach ($componentList as $key=>$components) { 
        $component1= ComponentPeer::retrieveByPK($components->getComponentId());?>
        <tr class="sf_admin_row_<?php echo $key%2?>">
            <input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="<? echo $component1->getId()?>"/>
            <input type="hidden" name="expdates[]" id ="expdates" value ="<?echo $components->getDate()?>">
            <td><?echo $component1->getProductcode()?></td>
            <td><?echo $component1->getDescription()?></td>
            <td><?echo $component1->getBrand()?></td>
            <td><?echo $component1->getComponentunit()?></td>
            <td><?echo $components->getDate()?></td>
            <td><input type="text" name="qtys[]" id ="qtys" size="5" value ="<?echo $components->getQty1()?>"></td>
        </tr>
<?php };

$bom = BOMPeer::retrieveByPK($mr->getBomId());
$c = new Criteria();
$c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
$c->addAscendingOrderByColumn(ComponentsPeer::COMPONENT_ID);
$compsList = ComponentsPeer::doSelect($c);
foreach ($compsList as $key1=>$comps) {
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $componentGroupId);
        $c->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId() );
        $co = ComponentsPeer::doSelectOne($c);
        if ($co) continue;
        $key++;
        $component1 =ComponentPeer::retrieveByPK($comps->getComponentId());
        if (!$component1){ echo "LOI xoa component". $bom->getComponentgroupId().'---'; echo $comps->getComponentId(); exit();}
        ?>
        <tr class="sf_admin_row_<?php echo $key%2?>">
            <input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="<? echo $component1->getId()?>"/>
            <input type="hidden" name="expdates[]" id ="expdates" value ="<?echo $components->getDate()?>">
            <td><?echo $component1->getProductcode()?></td>
            <td><?echo $component1->getDescription()?></td>
            <td><?echo $component1->getBrand()?></td>
            <td><?echo $component1->getComponentunit()?></td>
            <td><?echo $components->getDate()?></td>
            <td><input type="text" name="qtys[]" id ="qtys" size="5" value ="0"></td>
        </tr>
<?php };

}?>
</table>
</div>

<script type="text/javascript">

function changeList(){
    var bomId = $("mr_bom_id").value;
    var link = '<?php echo url_for('component/listCompByBOMAjax') ?>';
    new Ajax.Updater('list_component', link + '?bom_id='+ bomId, {asynchronous:true, evalScripts:false, onComplete:function(request, json){new Effect.Fade('indicator', {});new Effect.Highlight('list_component', {});}, onLoading:function(request, json){new Effect.Appear('indicator', {});}});
    }
</script>
  

<?php include_partial('edit_actions', array('mr' => $mr)) ?>

</form>

<ul class="sf_admin_actions">
      <li class="float-left"><?php if ($mr->getId()): ?>
<?php echo button_to(__('delete'), 'mr/delete?id='.$mr->getId(), array (
  'post' => true,
  'confirm' => __('Are you sure you want to delete this MR BOM?'),
  'class' => 'sf_admin_action_delete',
)) ?><?php endif; ?>
</li>
  </ul>
